//
//  MyCreateTribeListCell.m
//  yigou
//
//  Created by SunChao on 17/1/11.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "MyCreateTribeListCell.h"
#import "TribeHeader.h"

#define imgWidth 80
#define imgHeight 80
#define edge 15
@implementation MyCreateTribeListCell
-(void)setFrame:(CGRect)frame
{
    
        CGFloat kTableTopBorderWidth = 10;
        CGFloat kTableViewCellMargin = 10;
        frame.origin.y += kTableTopBorderWidth;
        frame.size.height -= kTableViewCellMargin;
    float imgW = (ScreenWidth - 15*5)/4;
    frame.size.width = ScreenW +imgW + 2*15;
    [super setFrame:frame];
    
}


-(void)contentWithArray:(NSArray *)array
{
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    self.array = [NSArray arrayWithArray:array];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, 150, 20)];
        titleLabel.text = @"我创建的部落";
    titleLabel.font = [UIFont systemFontOfSize:15];
    [self addSubview:titleLabel];
    
    float imgW = (ScreenWidth - 15*5)/4;
    
    for (int i = 0; i<array.count; i++) {
        NSDictionary *dic = [array objectAtIndex:i];
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(edge+(imgW+edge)*i, 30+20, imgW, imgW)];
        imgView.layer.cornerRadius = 8;
       imgView.layer.masksToBounds = YES;
        imgView.userInteractionEnabled = YES;
        imgView.tag = 100+i;
        [self addSubview:imgView];
       
        [imgView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"tribe_logo"]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];
        bgView.backgroundColor = [UIColor blackColor];
        bgView.alpha = 0.3;
       
        bgView.layer.cornerRadius = 8;
        bgView.layer.masksToBounds = YES;
        bgView.userInteractionEnabled = YES;
        imgView.userInteractionEnabled = YES;
        [imgView addSubview:bgView];
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, imgW/2-10, imgW, 20)];
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.tag = 200+i;
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.text = [dic objectForKey:@"tribe_name"];
        [nameLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
        [imgView addSubview:nameLabel];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        tapGesture.numberOfTapsRequired = 1;
        tapGesture.numberOfTouchesRequired = 1;
        [imgView addGestureRecognizer:tapGesture];
    }
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(edge + (imgW+edge)*array.count+2, 30+20, imgW, imgW)];
    imgView.layer.cornerRadius = 8;
    imgView.layer.masksToBounds = YES;
    imgView.image = [UIImage imageNamed:@"addTribeBg"];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.3;
    
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.userInteractionEnabled = YES;
    imgView.userInteractionEnabled = YES;
    imgView.image = [UIImage imageNamed:@"addTribeBg"];
    [imgView addSubview:bgView];
    
    imgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    tapGesture.numberOfTapsRequired = 1;
    tapGesture.numberOfTouchesRequired = 1;
    [imgView addGestureRecognizer:tapGesture];
    imgView.tag = 1000;

    
    UILabel *addLabel = [[UILabel alloc]init];
    addLabel.text = @"+";
    addLabel.textColor = [UIColor whiteColor];
    addLabel.textAlignment = NSTextAlignmentCenter;
    addLabel.font = [UIFont systemFontOfSize:60];
    addLabel.frame = CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height-20);
    [imgView addSubview:addLabel];

    
    UILabel *label = [[UILabel alloc]init];
    imgView.userInteractionEnabled = YES;
    label.frame = CGRectMake(0, imgView.frame.size.height-20, imgView.frame.size.width, 20);
    label.tag = 2000;
    label.text = @"创建部落";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:13];
    [imgView addSubview:label];
    [self addSubview:imgView];
    
    
}

- (void)tapGesture:(UITapGestureRecognizer *)sender{
    
    NSString *tribeID = nil;
    if (sender.view.tag == 1000) {
        tribeID = @"0";
    }else{
        NSInteger index = sender.view.tag-100;
        NSDictionary *dic = [self.array objectAtIndex:index];
        tribeID = [dic objectForKey:@"tribe_id"];
    }
    [self.delegate didClickImgWithTbibeID:tribeID];
}

@end
